package view.userinteface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.controller.ControllerPrincipal;
import beans.BeanCliente;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JInternalFrameAltaCliente extends javax.swing.JInternalFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnAceptar;
	private JTextField txtContacto;
	private JLabel lblContacto;
	private JButton btnCancelar;
	private JLabel lblTelefono;
	private JTextField txtPorcentajeDescuento;
	private JTextField txtTelefono;
	private JLabel lblDescuento;
	private JTextField txtRazonSocial;
	private JLabel lblRzSocial;
	private JTextField txtCUIT;
	private JLabel lblCUIT;
	private static JInternalFrameAltaCliente instancia;

	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
	
	public static JInternalFrameAltaCliente getInstancia(){
		if (instancia==null){
			instancia = new JInternalFrameAltaCliente();
		}
		return instancia;
	}
	
	public JInternalFrameAltaCliente() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(400, 290));
			this.setBounds(0, 0, 400, 290);
			setVisible(true);
			this.setTitle("Alta Cliente");
			getContentPane().setLayout(null);
			this.setSize(400, 400);
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar, "Center");
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(70, 207, 98, 26);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(validar()){
							BeanCliente beanCliente = new BeanCliente();
							beanCliente.setCuit(txtCUIT.getText());
							beanCliente.setContacto(txtContacto.getText());
							Float pjeDto = new Float("0");
							try{pjeDto = Float.parseFloat(txtPorcentajeDescuento.getText());}catch(Exception e){JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Porcentaje debe ser un numero");return;}
							if(pjeDto>100 || pjeDto<0){
								JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Porcentaje debe ser un numero entre 0 y 100");
								return;
							}
							beanCliente.setPorcentajeDesc(pjeDto);
							beanCliente.setRazonSocial(txtRazonSocial.getText());
							beanCliente.setTelefono(txtTelefono.getText());
							boolean result = ControllerPrincipal.getInstancia().altaCliente(beanCliente);
							if (result){
								JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"El cliente se ha grabado exitosamente");
								dispose();
							}
							else
								JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"No se pudo grabar el cliente");

						}else
							JOptionPane.showMessageDialog(JInternalFrameAltaCliente.getInstancia(),"Todos los campos son obligatorios");

					}
				}
						);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(206, 207, 107, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT: ");
				lblCUIT.setBounds(34, 19, 132, 14);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(179, 16, 166, 21);
			}
			{
				lblContacto = new JLabel();
				getContentPane().add(lblContacto);
				lblContacto.setText("Contacto:");
				lblContacto.setBounds(34, 166, 132, 14);
			}
			{
				txtContacto = new JTextField();
				getContentPane().add(txtContacto);
				getContentPane().add(getLblRzSocial());
				getContentPane().add(getTxtRazonSocial());
				getContentPane().add(getLblDescuento());
				getContentPane().add(getTxtPorcentajeDescuento());
				getContentPane().add(getLblTelefono());
				getContentPane().add(getTxtTelefono());
				txtContacto.setBounds(179, 163, 166, 21);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JLabel getLblRzSocial() {
		if(lblRzSocial == null) {
			lblRzSocial = new JLabel();
			lblRzSocial.setText("Razon Social");
			lblRzSocial.setBounds(34, 53, 145, 16);
		}
		return lblRzSocial;
	}
	
	private JTextField getTxtRazonSocial() {
		if(txtRazonSocial == null) {
			txtRazonSocial = new JTextField();
			txtRazonSocial.setBounds(179, 50, 166, 23);
		}
		return txtRazonSocial;
	}
	
	private JLabel getLblDescuento() {
		if(lblDescuento == null) {
			lblDescuento = new JLabel();
			lblDescuento.setText("Porcentaje Descuento");
			lblDescuento.setBounds(34, 91, 145, 16);
		}
		return lblDescuento;
	}
	
	private JTextField getTxtPorcentajeDescuento() {
		if(txtPorcentajeDescuento == null) {
			txtPorcentajeDescuento = new JTextField();
			txtPorcentajeDescuento.setBounds(179, 88, 166, 23);
		}
		return txtPorcentajeDescuento;
	}
	
	private JLabel getLblTelefono() {
		if(lblTelefono == null) {
			lblTelefono = new JLabel();
			lblTelefono.setText("Telefono");
			lblTelefono.setBounds(34, 127, 145, 16);
		}
		return lblTelefono;
	}
	
	private JTextField getTxtTelefono() {
		if(txtTelefono == null) {
			txtTelefono = new JTextField();
			txtTelefono.setBounds(179, 124, 166, 23);
		}
		return txtTelefono;
	}
	
	private boolean validar() {
		if("".equalsIgnoreCase(txtContacto.getText()) || "".equalsIgnoreCase(txtCUIT.getText()) || "".equalsIgnoreCase(txtPorcentajeDescuento.getText())
				|| "".equalsIgnoreCase(txtRazonSocial.getText()) || "".equalsIgnoreCase(txtTelefono.getText()))
			return false;
		else
			return true;
	}

}
